/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 *

 * <p>
 * 版权所有，侵权必究！
 */

package io.api.modules.login.controller;


import io.admin.common.exception.ErrorCode;
import io.admin.common.utils.ConvertUtils;
import io.admin.common.validator.AssertUtils;
import io.admin.common.utils.Result;
import io.admin.common.validator.ValidatorUtils;
import io.api.annotation.LoginUser;
import io.api.annotation.Pass;
import io.api.modules.login.dto.*;
import io.api.modules.login.entity.UserEntity;
import io.api.modules.login.service.UserService;
import io.api.modules.login.service.CaptchaService;
import io.api.modules.login.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 登录接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api")
@Api(tags = "登录接口")
@AllArgsConstructor
public class ApiLoginController {
    private final UserService userService;
    private final TokenService tokenService;
    private final CaptchaService captchaService;

    @Pass
    @GetMapping("captcha")
    @ApiOperation(value = "验证码")
    public Result captcha(String mobile) throws IOException {
        //uuid不能为空
        AssertUtils.isBlank(mobile, ErrorCode.IDENTIFIER_NOT_NULL);
        //生成验证码
        captchaService.create(mobile);
        return new Result().ok("验证码已发送");
    }

    @Pass
    @PostMapping("login")
    @ApiOperation("登录")
    public Result<Map<String, Object>> login(@RequestBody LoginDTO dto) {
        //表单校验
        ValidatorUtils.validateEntity(dto);
        //用户登录
        Map<String, Object> map = userService.login(dto);
        return new Result().ok(map);
    }

    @Pass
    @PostMapping("login/captcha")
    @ApiOperation("登录")
    public Result<Map<String, Object>> captchaLogin(@RequestBody CaptchaLoginDTO dto) {
        //表单校验
        ValidatorUtils.validateEntity(dto);
        //用户登录
        Map<String, Object> map = userService.captchaLogin(dto);
        return new Result().ok(map);
    }

    @PostMapping("logout")
    @ApiOperation("退出")
    public Result logout(@ApiIgnore @RequestAttribute("userId") Long userId) {
        tokenService.expireToken(userId);
        return new Result();
    }

    //@Login
    @Pass
    @PutMapping("password")
    @ApiOperation("修改密码")
    public Result passwordReset(@RequestBody PasswordDTO dto) {
        //表单校验
        ValidatorUtils.validateEntity(dto);
        userService.resetPassword(dto);
        return new Result();
    }

    @GetMapping("userInfo")
    @ApiOperation(value="获取用户信息", response= UserEntity.class)
    public Result<UserInfoDTO> userInfo(@ApiIgnore @LoginUser UserEntity user){
        UserInfoDTO userInfoDTO = ConvertUtils.sourceToTarget(user, UserInfoDTO.class);
        return new Result<UserInfoDTO>().ok(userInfoDTO);
    }


    @GetMapping("/project/member/{projectId}")
    @ApiOperation("信息")
    public Result<List<UserDTO>> member(@PathVariable("projectId") Long projectId){
        List<UserDTO> data = userService.selectByProjectId(projectId);
        if (Objects.isNull(data)) {
            return new Result().error(ErrorCode.NOT_NULL, "找不到对应数据");
        }
        return new Result<List<UserDTO>>().ok(data);
    }

}